package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.UserDao;
import com.itheima.domain.User;
import com.itheima.service.UserService;
import com.itheima.util.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class UserServiceImpl implements UserService {
    @Override
    public List<User> findAll() {
        //获取Dao对象
        SqlSession sqlSession = MyBatisUtils.openSession();
        UserDao userDao = sqlSession.getMapper(UserDao.class);

        //查询所有
        List<User> list = userDao.findAll();
        //释放资源
        sqlSession.close();
        return list;
    }

    @Override
    public void save(User user) {
        //获取Dao对象
        SqlSession sqlSession = MyBatisUtils.openSession();
        UserDao userDao = sqlSession.getMapper(UserDao.class);
        //添加用户
        userDao.save(user);
        //将角色和用户关联起来 //角色id，用于接收页面发送过来的内容roleIds;
        List<Integer> roleIds = user.getRoleIds();
        if (roleIds != null || roleIds.size() > 0) {
            //说明添加了角色，将用户id和角色id添加到中间表中
            for (Integer roleId : roleIds) {
                userDao .saveUserRoleRelation(user.getId(), roleId);
            }

        }
        //获取
        //提交事务
        sqlSession.commit();
        //释放资源
        sqlSession.close();
    }

    @Override
    public PageInfo<User> findByPage(Integer pageNum, Integer pageSize) {
        //获取Dao对象
        SqlSession sqlSession = MyBatisUtils.openSession();
        UserDao userDao = sqlSession.getMapper(UserDao.class);
        //1. 查询数据

        //分页
        PageHelper.startPage( pageNum , pageSize );
        //调用dao查询数据
        List<User> list = userDao.findList();

        //5. 释放资源
        sqlSession.close();
        return new PageInfo<>(list);
    }

    @Override
    public User findById(Integer id) {
        //获取Dao对象
        SqlSession sqlSession = MyBatisUtils.openSession();
        UserDao userDao = sqlSession.getMapper(UserDao.class);
        //查询
        User user = userDao.findById(id);
        //释放资源
        sqlSession.close();
        return user;
    }


    /**
     * 修改的方法
     * @param user
     */
    @Override
    public void update(User user) {
        //获取Dao对象
        SqlSession sqlSession = MyBatisUtils.openSession();
        UserDao userDao = sqlSession.getMapper(UserDao.class);
        //修改
        userDao.update(user);
        //先将原来的中间表对应关系删除，然后
        userDao.deleteUserRoleRelation(user.getId());
//重新维护，接收用户先填写的角色id集合
        List<Integer> roleIds = user.getRoleIds();
        //判断集合是否为空,如果为空则是用户没有添加角色或删除原来所有角色
        if (roleIds != null || roleIds.size() > 0) {
            //循环遍历
            for (Integer roleId : roleIds) {
                //将新的对应关系写入中间表  user.getId()主键返回的新用户id ，前台传过来的rid
                userDao.saveUserRoleRelation(user.getId(),roleId);
            }
        }

        //提交事务
        sqlSession.commit();
        //释放资源
        sqlSession.close();
    }

    @Override
    public void delete(Integer id) {
        //获取dao对象
        SqlSession sqlSession = MyBatisUtils.openSession();
        UserDao userDao = sqlSession.getMapper(UserDao.class);
        //删除用户和角色关联关系
        userDao.deleteUserRoleRelation(id);
        //删除
        userDao.delete(id);
        //提交事务
        sqlSession.commit();
        //释放资源
        sqlSession.close();
    }
}
